<?php

/**
 *
 *
 * Wrapper for Setting and TemplateSetting model
 * 	to retrieve(not save) setting values from Twig/actions/layout
 *
 *
 */
class Settings implements ArrayAccess {
	protected $settingModel;
	protected $defaults;

	// Singleton
    protected static $_instance;
    public static function getInstance(Setting $settingModel=null){
		if (!(self::$_instance instanceof self)){
			self::$_instance = new self($settingModel);
		}
		return self::$_instance;
    }


	public function __construct(Setting $settingModel=null){
        if ($settingModel==""){
		    $this->initSettingModel();
        }else{
            $this->settingModel=$settingModel;
        }
		$this->initDefaults();
	}

	public function initSettingModel(){
		$this->settingModel=Doctrine::getTable("Setting")->getLast();
		if (empty($this->settingModel)){
			$this->settingModel=new Setting();
		}
	}

	public function initDefaults(){
		$settingForm=new SettingForm();
		$this->defaults=array_change_key_case($settingForm->getDefaults());
	}

	public function getValue($name, $default=null){
		$name=strtolower($name);
		$out=null;
		$modelValue=$this->settingModel->getValue($name);
		if (!empty($modelValue)){
			$out=$modelValue;
		}else{ // get from defaults
			if (array_key_exists($name, $this->defaults)){
				$out=$this->defaults[$name];
			}
		}

		if ($out!="") return $out;
		if ($default!="") return $default;
	}

/*
	public function getValue($name, $default=null){
		$name=strtolower($name);

		$values=$this->getValues();
		if (array_key_exists($name, $values)){
			return $values[$name];
		}

		if ($default!="") return $default;
	}
*/
	public function getValues(){
		$modelValues=$this->settingModel->getValues();
		return array_merge($this->defaults, $modelValues);
	}


	public function getSettingModel(){
		return $this->settingModel;
	}


	// ArrayAccess implementation
	public function offsetSet($offset, $value) {
		return false;
	}
	public function offsetUnset($var) {
		return false;
	}

    public function offsetExists($var) {
    	if ($this->getValue($var)!=""){
    		return true;
    	}

    	return false;
    }
	public function offsetGet($var) {
		return $this->getValue($var);
	}
	// /ArrayAccess implementation


	/**
	 *
	 * methods to get through Twig "." syntax with our setting dotted names
	 *
	 */
	public function __get($name){
		$name=strtolower($name);
		$out=new ObjectForTwig();
//var_dump($this->getValues());
		foreach ($this->getValues() as $key => $value){
			if (strpos($key, $name)===0){
				$settingKeys=explode(".", $key);
				unset($settingKeys[0]);
				$out->$settingKeys[1] = $value;
			}
		}

		// cache
		$this->$name=$out;

		return $out;
	}

	public function __isset($name){
		$name=strtolower($name);
		foreach ($this->getValues() as $key => $value){
			if (strpos($key, $name)===0){
				return true;
			}
		}
		return false;
	}



}



/**
 *
 * object to get through Twig "." syntax with our setting dotted names
 *
 */
class ObjectForTwig {
	public function __get($name){
		$name=strtolower($name);
		return $this->$name;
	}

	public function __isset($name){
		$name=strtolower($name);
		if (isset($this->$name)){
			return true;
		}
	}
}





